<?php
class Article extends AppModel {

    public $validate = array(
    'title' => array('rule'=>array('between',1,150),'message'=>'Between 1 and 150 characters'),
    'seo_link' => array(
    'isUnique'=>array(
    'rule' => 'isUnique',
    'message' => 'Seo link already taken'),
    'notEmpty'=>array(
    'rule'=>'notEmpty',
    'message'=>'This field cannot be left blank')
    ),
    'text' => array('rule'=>array('notEmpty'),'message'=>'This field cannot be left blank'),
    'intro_text' => array('rule'=>array('notEmpty'),'message'=>'This field cannot be left blank'),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    public $belongsTo = array(
    'User' => array('className' => 'User',
    'foreignKey' => 'user_id',
    'conditions' => '',
    'fields' => 'name,username,email',
    'order' => '',
    'counterCache' => ''),
    'Category' => array('className' => 'Category',
    'foreignKey' => 'category_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'counterCache' => ''),
    );

    public $hasMany = array(
    'Comment' => array('className' => 'Comment',
    'foreignKey' => 'article_id',
    'conditions' => '(Comment.antispam ="" or Comment.antispam is null)',
    'fields' => '',
    'order' => 'Comment.created',
    'limit' => '',
    'offset' => '',
    'dependent' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''),
    'Rating' => array('className' => 'Rating',
    'foreignKey' => 'article_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'dependent' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''),
    );

    public $hasAndBelongsToMany = array(
    'Tag' => array('className' => 'Tag',
    'joinTable' => 'articles_tags',
    'foreignKey' => 'article_id',
    'associationForeignKey' => 'tag_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'unique' => '',
    'finderQuery' => '',
    'deleteQuery' => '',
    'insertQuery' => ''),
    );

    public function update_visited($seo_link, $created) {
        return $this->updateAll(array('visited'=>'visited+1'),array('Article.seo_link'=>$seo_link,'Article.created like "'.$created.'"'));
    }

    public function find_by_seo_and_date($seo_link, $created) {
        return $this->find(array('Article.seo_link'=>$seo_link,'Article.created like "'.$created.'"'));
    }

    public function recent($category_id) {
        return $this->findAll(array('Article.category_id'=>$category_id),array('Article.title','Article.created','Article.seo_link'),'Article.created desc',5,1,0);
    }

    public function best() {
        return $this->findAll(array('Article.visible=1','Article.created<=now()'),array('seo_link','title','created'),'Article.visited desc',5,1,0);
    }
}
?>